package zl.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTest {
	
	public static void main(String[] args){
		Connection conn = null;		//数据库连接对象
		Statement stmt = null;		//数据库操作对象
		ResultSet result = null;	//数据库结果集对象
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");	//加载驱动
			conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");	//得到数据库连接
			stmt = conn.createStatement();	//创建一个数据库操作对象
			result = stmt.executeQuery("select count(*) from user_users");	//执行查询并将查询结果赋给结果集对象
//			PreparedStatement pstmt = conn.prepareStatement("select count(*) from user_users");
//			result = pstmt.executeQuery();
			while(result.next()){	//循环读取结果集
				System.out.println(result.getInt(1));
			}
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}
		try {
			//关掉资源连接
			if(result != null && !result.isClosed()){
				result.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(stmt != null && !stmt.isClosed()){
				stmt.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(conn != null && !conn.isClosed()){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}
